/**
 * Created with IntelliJ IDEA.
 * Description:编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址
 * User: wangxin
 * Date: 2025-04-10
 * Time: 22:04
 */
public class Test2 {
    public boolean isIPv4(String IP){
        if(IP.indexOf(".") == -1){
            return false;
        }
        String[] s = IP.split("\\.");
        if(s.length != 4){
            return false;
        }
        for(int i = 0;i < s.length;i++){
            if(s[i].length() == 0){
                return false;
            }
            if(s[i].length() < 0||s[i].length() > 3||(s[i].charAt(0) == '0'&&s[i].length() != 1)){
                return false;
            }
            int num = 0;
            for(int j = 0;j < s[i].length();j++){
                char c= s[i].charAt(j);
                if(c < '0'||c >'9'){
                    return false;
                }
                num = num*10 + (int)(c-'0');
                if(num < 0||num >255){
                    return false;
                }
            }
        }
        return true;
    }
    public boolean isIPv6(String IP){
        if(IP.indexOf(":") == -1){
            return false;
        }
        String[] s = IP.split(":",-1);
        if(s.length != 8){
            return false;
        }
        for(int i = 0;i < s.length;i++){
            if(s[i].length() == 0||s[i].length() > 4){
                return false;
            }
            for(int j = 0;j < s[i].length();j++){
                char c = s[i].charAt(j);
                boolean b = (c >= '0'&&c <= '9')||(c >= 'a'&&c <= 'f')||(c >= 'A'&&c <= 'F');
                if(!b){
                    return false;
                }
            }
        }
        return true;
    }
    public String solve (String IP) {
        // write code here
        if(isIPv4(IP)){
            return "IPv4";
        }else if(isIPv6(IP)){
            return "IPv6";
        }else{
            return "Neither";
        }
    }
}
